package com.szqhyxj.youxiangju.biz.merchantHome.plan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.plan.adapter.PlanAdapter;
import com.szqhyxj.youxiangju.biz.merchantHome.plan.particulars.PlanParticularsActivity;
import com.szqhyxj.youxiangju.biz.plan.PlanList;
import com.szqhyxj.youxiangju.biz.plan.RowItem;
import com.szqhyxj.youxiangju.biz.plan.RowsItem;
import com.szqhyxj.youxiangju.biz.plan.bridge.PlanDetailBridge;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.biz.web.WebExit;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * XiaoPanYu
 * on 2018/11/10
 */
public class PlanListFragment extends BasePromptFragment implements PlanListContract.View, PlanAdapter.ItemClickListener {

    @BindView(R.id.paln_list)
    ListView palnList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    Unbinder unbinder;

    private static final String PLANTYPE = "plan_type";
    @BindView(R.id.none)
    LinearLayout none;
    private PlanListPresenter presenter;

    public void setSetListSize(SetListSize setListSize) {
        this.setListSize = setListSize;
    }

    private SetListSize setListSize;
    private PlanAdapter adapter;
    private int type;
    private List<RowItem> rowsItemList;
    private boolean isStart = false;

    public static PlanListFragment newInstance(int planType) {
        Bundle args = new Bundle();
        args.putInt(PLANTYPE, planType);
        PlanListFragment fragment = new PlanListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_plan_commonality;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt(PLANTYPE);
        rowsItemList = new ArrayList<>();
        adapter = new PlanAdapter(rowsItemList, type);
        adapter.setListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isStart) {
            presenter.init(type);
        }

    }

    @Override
    protected void start() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        if (presenter == null) {//避免重复获取
            presenter = new PlanListPresenter(this);
            palnList.setAdapter(adapter);
            initViews();
            presenter.init(type);
            isStart = true;
        }
        return rootView;
    }

    private void initViews() {
        swipeLayout.setColorSchemeResources(R.color.colorPrimaryDark);
        swipeLayout.setProgressViewOffset(true, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.init(type);
                showRefresh(true);
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void showPlanList(List<RowItem> list) {

        if (list.size() == 0) {
            none.setVisibility(View.VISIBLE);
            palnList.setVisibility(View.GONE);
        } else {
            none.setVisibility(View.GONE);
            palnList.setVisibility(View.VISIBLE);
        }

        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
        rowsItemList.clear();
        rowsItemList.addAll(list);

        if (setListSize != null) {
            setListSize.setListSize(rowsItemList.size());
        }

        adapter.notifyDataSetChanged();
    }

    public interface SetListSize {
        void setListSize(int size);
    }

    @Override
    public void showNormalPaln() {
    }

    @Override
    public void showErrPlan() {
    }

    @Override
    public void showNone() {
    }

    @Override
    public void showRefresh(boolean isRefresh) {
        if (swipeLayout == null) {
            return;
        }
        swipeLayout.setRefreshing(isRefresh);
    }

    @Override
    public void onClickItem(int i) {
        PlanDetailBridge.showPlanDetail(getActivity(), rowsItemList.get(i).getPlanId(), WebExit.FINISH.getCode());
    }


    public void setList() {
        presenter.init(type);
    }


}
